﻿using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;
using WhatCatch.Core2.Model;
using WhatCatch.Core2.Repository;

namespace WhatCatch.Core2
{
    public class DataContainer
    {
        static object lockQueue = new object();
        static object lockUrl = new object();
        ConcurrentQueue<CatchTask> _queueTask;
        Hashtable hashUrl;

        public DataContainer()
        {
            _queueTask = new ConcurrentQueue<CatchTask>();
            hashUrl = new Hashtable();
        }

        ///// <summary>
        ///// 获取资源信息
        ///// </summary>
        ///// <returns></returns>
        //public Resource GetResourceInfo(long id)
        //{
        //    var site = DB.GetSite(id);
        //    var siteConfig = DB.GetSiteConfig(id);
        //    Resource resource = new Resource
        //    {
        //        ID = site.ID,
        //        Name = site.Name,
        //        Url = site.Url,
        //        ResourceConfig = new ResourceConfig
        //        {
        //            ListXPath = siteConfig.ListXPath,
        //            ListItemXPath = siteConfig.ListItemXPath,
        //            ListItemUrlXPath = siteConfig.ListItemUrlXPath,
        //            PageXPath = siteConfig.PageXPath,
        //            PageItemXPath = siteConfig.PageItemXPath,
        //            PageItemUrlXPath = siteConfig.PageItemUrlXPath,
        //            DetailXPath = siteConfig.DetailXPath,
        //            DetailTitleXPath = siteConfig.DetailTitleXPath,
        //        }
        //    };
        //    return resource;
        //}

        public void AddListUrl(string url,int type)
        {
            lock (lockUrl)
            {
                //判重
                if (hashUrl.ContainsKey(url))
                    return;

                //listUrl.Add(url);
                hashUrl.Add(url, 0);
            }

            //入任务队列
            lock (lockQueue)
            {
                _queueTask.Enqueue(new CatchTask
                {
                    Url = url,
                    HandleType = type,
                    //ConfigId = 0
                });
            }
        }

        public CatchTask GetOneTask()
        {
            CatchTask url;
            lock (lockQueue)
            {
                if (_queueTask.TryDequeue(out url) == false)
                {
                    return null;
                }
            }
            return url;
        }

        //static object lockDetails = new object();
        //List<AnalyserContentResponse> details = new List<AnalyserContentResponse>();
        //public void AddDetails(AnalyserContentResponse detail)
        //{
        //    lock (lockDetails)
        //    {
        //        details.Add(detail);
        //    }
        //}
        //public List<AnalyserContentResponse> GetAllDetails()
        //{
        //    return details;
        //}
    }
}
